            
class TaskController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ taskList: Task.list( params ) ]
    }

    def show = {
        def task = Task.get( params.id )

        if(!task) {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ task : task ] }
    }

    def delete = {
        def task = Task.get( params.id )
        if(task) {
            task.delete()
            flash.message = "Task ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def task = Task.get( params.id )

        if(!task) {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ task : task ]
        }
    }

    def update = {
        def task = Task.get( params.id )
        if(task) {
            task.properties = params
            if(!task.hasErrors() && task.save()) {
                flash.message = "Task ${params.id} updated"
                redirect(action:show,id:task.id)
            }
            else {
                render(view:'edit',model:[task:task])
            }
        }
        else {
            flash.message = "Task not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def task = new Task()
        task.properties = params
        return ['task':task]
    }

    def save = {
        def task = new Task(params)
        if(!task.hasErrors() && task.save()) {
            flash.message = "Task ${task.id} created"
            redirect(action:show,id:task.id)
        }
        else {
            render(view:'create',model:[task:task])
        }
    }
}